package com.teaching.teachingsupport.controller.teachercontroller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teaching.teachingsupport.common.api.Result;
import com.teaching.teachingsupport.common.enums.HomeworkStatus;
import com.teaching.teachingsupport.common.pojo.dto.request.CreateHomeworkRequest;
import com.teaching.teachingsupport.common.pojo.dto.response.CourseResponse;
import com.teaching.teachingsupport.common.pojo.dto.response.HomeworkListResponse;
import com.teaching.teachingsupport.common.pojo.dto.response.HomeworkScoreDetailResponse;
import com.teaching.teachingsupport.common.pojo.dto.response.TeachingClassResponse;
import com.teaching.teachingsupport.service.service2.teacherservice.TeacherHomeworkService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.List;

/**
 * 教师作业管理接口
 */
@RestController
@RequestMapping("/api/teacher/homework")
@RequiredArgsConstructor
public class TeacherHomework {
    
    private final TeacherHomeworkService teacherHomeworkService;

    /**
     * 创建作业
     */
    @PostMapping("/create")
    public Result<Long> createHomework(
            @RequestPart(name = "homeworkData") CreateHomeworkRequest request,
            @RequestPart(required = false) MultipartFile attachment
    ) {
        return Result.success(teacherHomeworkService.createHomework(request, attachment));
    }

    /**
     * 获取教师课程列表
     */
    @GetMapping("/course-list")
    public Result<List<CourseResponse>> getTeacherCourses(
            @RequestParam(required = false) String courseName
    ) {
        return Result.success(teacherHomeworkService.getTeacherCourses(courseName));
    }

    /**
     * 获取教学班级列表
     */
    @GetMapping("/class-list")
    public Result<List<TeachingClassResponse>> getTeachingClasses(
            @RequestParam Long courseId,
            @RequestParam(required = false) String className
    ) {
        return Result.success(teacherHomeworkService.getTeachingClasses(courseId, className));
    }

    /**
     * 获取作业列表
     * @param status 作业状态（可选）
     * @param courseName 课程名称（可选）
     * @param pageNum 页码（默认1）
     * @param pageSize 每页大小（默认10）
     * @return 分页的作业列表
     */
    @GetMapping("/list")
    public Result<Page<HomeworkListResponse>> getHomeworkList(
            @RequestParam(required = false) HomeworkStatus status,
            @RequestParam(required = false) String courseName,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        return Result.success(teacherHomeworkService.getHomeworkList(status, courseName, pageNum, pageSize));
    }

    /**
     * 计算学生作业得分，返回学生选择题+文字题的作业
     * @param homeworkId 作业ID
     * @param studentId 学生ID
     * @return 作业得分详情，包含总分和每道题的得分情况
     */
    @GetMapping("/{homeworkId}/student/{studentId}/score")
    public Result<HomeworkScoreDetailResponse> calculateHomeworkScore(
            @PathVariable Long homeworkId,
            @PathVariable Long studentId
    ) {
        return Result.success(teacherHomeworkService.calculateHomeworkScore(homeworkId, studentId));
    }
        //ToDO：学生端需验证homework_teacher中的status 来判断是否可以查看作业详情
    /**
     * 教师为简答题评分
     * @param homeworkId 作业ID
     * @param studentId 学生ID
     * @param questionId 题目ID
     * @param score 分数
     * @return 更新后的作业得分详情
     */
    @PutMapping("/{homeworkId}/student/{studentId}/question/{questionId}/text-score")
    public Result<HomeworkScoreDetailResponse> gradeTextQuestion(
            @PathVariable("homeworkId") Long homeworkId,
            @PathVariable("studentId") Long studentId,
            @PathVariable("questionId") Long questionId,
            @RequestParam("score") BigDecimal score) {
        HomeworkScoreDetailResponse response = teacherHomeworkService.gradeTextQuestion(homeworkId, studentId, questionId, score);
        return Result.success(response);
    }

}
